package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.OrderService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

import static com.sky.entity.Orders.*;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO selectB(LocalDateTime begin, LocalDateTime end) {
//        今日新增用户
        Long dayIncr = userMapper.selectU(begin, end);
        //有效订单数量
        Integer okOrder = Math.toIntExact(reportMapper.selectO(begin, end, COMPLETED));
        //订单总数
        Integer totalOrder = Math.toIntExact(reportMapper.selectO(begin, end, null));
        //订单完成率
        Double ll = 0d;
        if (totalOrder != null && totalOrder != 0) {
            ll = okOrder.doubleValue() / totalOrder;
        }
        //营业额
        BigDecimal bigDecimal = reportMapper.selectT(begin, end, COMPLETED);
        bigDecimal = bigDecimal == null ? new BigDecimal(0) : bigDecimal;
        Double turnover = bigDecimal.doubleValue();
        //客单价
        Long u = reportMapper.selectU(begin, end);
        //        个数
        BigDecimal bigDecimal1 = new BigDecimal(u);

        BigDecimal divide = u == 0 ? new BigDecimal(0) : bigDecimal.divide(bigDecimal1, 2, RoundingMode.HALF_UP);


        BusinessDataVO businessDataVO = new BusinessDataVO(turnover, okOrder, ll, divide.doubleValue(), dayIncr.intValue());
        return businessDataVO;
    }

    @Override
    public OrderOverViewVO getOrderOverView() {

        //待接单
        Integer waitingOrders = Math.toIntExact(reportMapper.selectO(LocalDateTime.now().with(LocalTime.MIN), null, TO_BE_CONFIRMED));

        //待派送

        Integer deliveredOrders = Math.toIntExact(reportMapper.selectO(LocalDateTime.now().with(LocalTime.MIN), null, CONFIRMED));

        //已完成

        Integer completedOrders = Math.toIntExact(reportMapper.selectO(LocalDateTime.now().with(LocalTime.MIN), null, COMPLETED));

        //已取消

        Integer cancelledOrders = Math.toIntExact(reportMapper.selectO(LocalDateTime.now().with(LocalTime.MIN), null, CANCELLED));


        //全部订单

        Integer allOrders = Math.toIntExact(reportMapper.selectO(LocalDateTime.now().with(LocalTime.MIN), null, null));

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }


}
